<?php
 /**
 * @package    art
 * @copyright  Copyright (c) 2013 LaZaDa SEA
 * @author     long.nguyen-thanh <long.nguyen-thanh@lazada.com
 * @date       10/19/14 10:09 AM
 */
class Zendvn_Paypal extends Zend_Http_Client
{
    private $_api_version = '93';
    private $_url = 'https://api-3t.sandbox.paypal.com/nvp';
    private $_url_checkout = 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=';
    private $_api_username = 'longthanh1705-facilitator_api1.gmail.com'; // These differ depending if you're in live mode or test mode.
    private $_api_password = 'BUFUXHNFZPRQ3BY7'; // I'm certain you can come up with something to take that into account. :-)
    private $_api_signature = 'AFcWxV21C7fd0v3bYYYRCpSSRl31A4iBsRHM4EEibqShbgk.sAsUPG7.';

    public function __construct()
    {
        parent::__construct($this->_url);

        // NOTE: Parameters must always be url encoded, as per PayPal documentation.
        $this->setParameterGet('USER', urlencode($this->_api_username));
        $this->setParameterGet('PWD', urlencode($this->_api_password));
        $this->setParameterGet('SIGNATURE', urlencode($this->_api_signature));
        $this->setParameterGet('VERSION', urlencode($this->_api_version));
    }

    public function setExpressCheckout($value){
        $this->setParameterGet('METHOD', 'SetExpressCheckout');
        $this->setParameterGet('PAYMENTREQUEST_0_PAYMENTACTION', 'SALE');
        $this->setParameterGet('PAYMENTREQUEST_0_AMT', $value);
        $this->setParameterGet('PAYMENTREQUEST_0_CURRENCYCODE', 'USD');
        $this->setParameterGet('cancelUrl', 'http://artserv.org/');
        $this->setParameterGet('returnUrl', 'http://artserv.org/?show=1');

        $result = $this->request(Zend_Http_Client::GET)->getBody();

        $result = explode('&',$result);
        if(count($result)>1){
            $result = explode('=',current($result));
            return $this->_url_checkout.$result[1];

        }else{
            return '/index';
        }
    }

}